<?php

namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Models\AfterSales;
use App\Models\Order;
use App\Util\Tool;
use Illuminate\Http\Request;
use mysql_xdevapi\Exception;
/**
 * 此项目由百凯伦公司研发，后面省略一万字
 */
class RefuseController extends Controller
{
    //拒绝退款
    public function store(Request $request)
    {
        $id = $request->id ?? 0;
        $cause = $request->cause ?? null; //拒绝原因
        $aftersales = new AfterSales();
        $row = $aftersales->where('id', $id)->first();
        $orderId = $row->order_id ?? '';
        $orderObj = new Order();
        $order = $orderObj->where([['id', '=', $orderId]])->first();
        //$order->order_status = 1;//订单设置已完成
        $row->status = 2;       //驳回
        $row->after_status = 0; //未完成
        $row->reject_reason = $cause;
        $row->reject_time = date("Y-m-d H:i:s",time());
        try {
            $row->save();
            $order->save();
            $dt["user_id"] =$order->user_id;
            $dt["title"] = "拒绝售后消息";
            $dt["content"] = "您的售后申请被拒绝，详情点击查看。";
            Tool::startPush($dt);
        } catch (Exception $exception) {
            return $exception->getMessage();
        }
        $data = [
            'code' => 1,
            'id' => $id,
            'msg' => "拒绝退款",
            'data' => ['refuse' => $cause],
        ];
        return response()->json($data);
    }
}
